package com.tanhua.server.controller;

import com.tanhua.commons.utils.UserHolder;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentsController {
    @Autowired
    private CommentsService commentsService;

    @Autowired
    private UserFreezeService userFreezeService;

    @PostMapping//发布评论
    public ResponseEntity saveComments(@RequestBody Map map){
        Long userId = UserHolder.getUserId();
        userFreezeService.checkUserStatus("2",userId);
        String movementId =(String) map.get("movementId");
        String comment = (String)map.get("comment");
        commentsService.saveComments(movementId,comment);
        return ResponseEntity.ok(null);
    }

    @GetMapping//查看评论
    public ResponseEntity findComments(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       String movementId){
       PageResult pr= commentsService.findComments(page,pagesize,movementId);
       return ResponseEntity.ok(pr);
    }

    @GetMapping("/{id}/like")//评论id不是用户的id!
    public ResponseEntity like(@PathVariable("id") String userId){
        Integer counts=commentsService.commentsLike(userId);
        return ResponseEntity.ok(counts);
    }

    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String userId){
        Integer counts=commentsService.commentsDisLike(userId);
        return ResponseEntity.ok(counts);
    }
}
